package com.zds.hj.poseidon.biz.common;

import com.alibaba.fastjson.JSON;
import com.zds.boot.common.context.ZDSContext;
import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.common.facade.RequestBase;
import com.zds.boot.saturn.log.point.support.BizLogPointCutSupport;
import com.zds.component.files.facade.dto.FileAuthDto;
import com.zds.hj.competency.facade.management.dto.developer.PlatformBasicDto;
import com.zds.hj.competency.facade.management.dto.protocol.ProtocolFillReqDto;
import com.zds.hj.competency.facade.management.dto.protocol.ProtocolInsRespDto;
import com.zds.hj.competency.facade.management.dto.protocol.ProtocolSignReqDto;
import com.zds.hj.competency.facade.management.dto.serial.SerialInfoDto;
import com.zds.hj.competency.facade.management.interfaces.PlatformBasicInterface;
import com.zds.hj.competency.facade.management.interfaces.ProtocolInsInterface;
import com.zds.hj.competency.facade.management.interfaces.SerialGenerateInterface;
import com.zds.hj.competency.facade.mapping.dto.syxapi.invoice.InvoiceAuthorizedReqDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.invoice.InvoiceAuthorizedRespDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.ocr.*;
import com.zds.hj.competency.facade.mapping.dto.syxapi.sign.OpenApiResponseDto;
import com.zds.hj.competency.facade.mapping.dto.syxapi.sign.RequestPerCreateAccountDto;
import com.zds.hj.competency.facade.mapping.interfaces.HjCompetencyPlatformInterface;
import com.zds.hj.crm.facade.interfaces.customer.EnterpriseCustomerInterface;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.req.EnterpriseRealnameVerifyReqDto;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.resp.AuthRespDto;
import com.zds.hj.icmc.facade.auth.assetcenter.dto.resp.EnterpriseRealnameVerifyRespDto;
import com.zds.hj.icmc.facade.interfaces.auth.AuthInterface;
import com.zds.user.facade.pbac.enums.UserTypeEnum;
import com.zds.zly.common.constant.CommConstant;
import com.zds.zly.common.enums.HjResultCodeEnum;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @Author duanhao
 * @Description 能力服务的接口
 * @Date 2023/5/30 15:13
 */
@Slf4j
@Service
public class CompetencyServiceInterface {
    @DubboReference(owner = CommConstant.DUBBO_OWNER, version = CommConstant.DUBBO_VERSION)
    private SerialGenerateInterface serialGenerateInterface;
    @DubboReference(owner = CommConstant.DUBBO_OWNER, version = CommConstant.DUBBO_VERSION, timeout = 9999999)
    private HjCompetencyPlatformInterface hjCompetencyPlatformInterface;
    @DubboReference(owner = CommConstant.DUBBO_OWNER, version = CommConstant.DUBBO_VERSION, timeout = 9999999)
    private ProtocolInsInterface protocolInsInterface;
    @DubboReference(version = CommConstant.DUBBO_VERSION, owner = CommConstant.DUBBO_OWNER)
    private PlatformBasicInterface platformBasicInterface;

    @DubboReference(owner = CommConstant.DUBBO_OWNER, version = CommConstant.DUBBO_VERSION, timeout = 9999999)
    private AuthInterface authInterface;

    /**
     * 企业实名认证
     * @param dto
     * @return
     */
    public AuthRespDto enterpriseCompanyRealName(EnterpriseRealnameVerifyReqDto dto) {
        AuthRespDto<EnterpriseRealnameVerifyRespDto> authRespDto = authInterface.enterpriseRealNameVerify(dto);
        return authRespDto;
    }


    /**
     * 生成系统流水号
     * @param code
     * @return
     */
    public String generateSerialNo(String code) {
        return serialGenerateInterface.generate(SerialInfoDto.fromCode(code));
    }

    /**
     * 身份证识别
     * @param reqDto
     * @return
     */
    public OcrDistingIDCardRespDto ocrIdCard(OcrDistingIDCardReqDto reqDto) {
        return hjCompetencyPlatformInterface.ocrIdCard(reqDto);
    }

    /**
     * 获取税务授权地址
     * @param request
     * @return
     */
    public InvoiceAuthorizedRespDto entAuthorizedAddress(InvoiceAuthorizedReqDto request) {
        return hjCompetencyPlatformInterface.entAuthorizedAddress(request);
    }


    /**
     * 营业执照识别
     * @param reqDto
     * @return
     */
    public OcrDistingEnterpriseRespDto ocrBizLicence(OcrDistingReqDto reqDto) {
        return hjCompetencyPlatformInterface.ocrBizLisence(reqDto);
    }

    /**
     * 银行卡识别
     * @param reqDto
     * @return
     */
    public OcrDistingBankCardRespDto ocrBankCard(OcrDistingReqDto reqDto) {
        return hjCompetencyPlatformInterface.ocrBankCard(reqDto);
    }

    /**
     * 协议填充
     * @param reqDto
     * @return
     */
    public ProtocolInsRespDto protocolFill(ProtocolFillReqDto reqDto) {
        try {
            log.info("【能力服务调度】>>> 协议填充 【开始】>>>【入参】{}", reqDto);
            ZDSContext.SeUserInfo seUserInfo = ZDSContext.get().getSeUserInfo();
            FileAuthDto.FileAuthBuilder fileAuthBuilder = FileAuthDto.builder().buildUserType(UserTypeEnum.PLATFORM.code());
            if (seUserInfo != null) {
                fileAuthBuilder.buildUserId(seUserInfo.getId());
            }
            ProtocolInsRespDto result = protocolInsInterface.protocolFill(reqDto, fileAuthBuilder.build());
            log.info("【能力服务调度】>>> 协议填充 【结束】>>>【出参】{}", JSON.toJSONString(result));
            return result;
        } catch (BusinessException e) {
            log.info("【能力服务调度】>>> 协议填充异常", e);
            throw e;
        } catch (Exception ex) {
            log.info("【能力服务调度】>>> 协议填充异常", ex);
            throw new BusinessException("调用能力服务异常", HjResultCodeEnum.SERVER_ERR.code());
        }
    }

    /**
     * 查找商家信息
     * @param requestBase
     * @return
     */
    public PlatformBasicDto findPlatformInfo(RequestBase requestBase) {
        try {
            log.info("【能力服务调度】>>> 查询平台信息 【开始】>>>【入参】{}", requestBase);
            PlatformBasicDto platformInfo = platformBasicInterface.findPlatformBasicInfo();
            log.info("【能力服务调度】>>> 查询平台信息 【结束】>>>【出参】{}", platformInfo);
            if (EmptyChecker.isEmpty(platformInfo)) {
                throw new BusinessException("调用能力异常，查询数据为空", HjResultCodeEnum.SERVER_ERR.code());
            }
            return platformInfo;
        } catch (BusinessException e) {
            log.error("【能力服务调度】>>> 查询平台信息异常", e);
            BizLogPointCutSupport.create(null, null, "查询平台信息", "查询平台信息业务异常", e);
            throw e;
        } catch (Exception ex) {
            log.error("【能力服务调度】>>> 查询平台信息异常", ex);
            BizLogPointCutSupport.create(null, null, "查询平台信息", "查询平台信息内部异常", ex);
            throw new BusinessException("查询平台信息异常", HjResultCodeEnum.SERVER_ERR.code());
        }
    }

    /**
     * 企业法人开户
     * @param reqDto
     * @return
     */
    public OpenApiResponseDto personalOpenChapter(RequestPerCreateAccountDto reqDto) {
        try {
            log.info("【能力服务调度】>>> 企业法人开户 【开始】>>>【入参】{}", reqDto);
            OpenApiResponseDto result = hjCompetencyPlatformInterface.personalOpenChapter(reqDto);
            log.info("【能力服务调度】>>> 企业法人开户 【结束】>>>【出参】{}", JSON.toJSONString(result));
            return result;
        } catch (BusinessException e) {
            log.info("【能力服务调度】>>> 企业法人开户异常", e);
            throw e;
        } catch (Exception ex) {
            log.info("【能力服务调度】>>> 企业法人开户异常", ex);
            throw new BusinessException("调用能力服务异常", HjResultCodeEnum.SERVER_ERR.code());
        }
    }

    /**
     * 协议签章
     * @param reqDto
     * @return
     */
    public ProtocolInsRespDto protocolSignature(ProtocolSignReqDto reqDto) {
        try {
            log.info("【能力服务调度】>>> 协议签章 【开始】>>>【入参】{}", reqDto);
            ZDSContext.SeUserInfo seUserInfo = ZDSContext.get().getSeUserInfo();
            FileAuthDto.FileAuthBuilder fileAuthBuilder = FileAuthDto.builder().buildUserType(UserTypeEnum.PLATFORM.code());
            if (seUserInfo != null) {
                fileAuthBuilder.buildUserId(seUserInfo.getId());
            }
            ProtocolInsRespDto result = protocolInsInterface.protocolSignature(reqDto, fileAuthBuilder.build());
            log.info("【能力服务调度】>>> 协议签章 【结束】>>>【出参】{}", JSON.toJSONString(result));
            return result;
        } catch (BusinessException e) {
            log.info("【能力服务调度】>>> 协议签章异常", e);
            throw e;
        } catch (Exception ex) {
            log.info("【能力服务调度】>>> 协议签章异常", ex);
            throw new BusinessException("调用能力服务异常", HjResultCodeEnum.SERVER_ERR.code());
        }
    }


}
